<!doctype html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <!-- 必须的 meta 标签 -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap 的 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="../../../static/css/main.css" th:href="@{/static/css/main.css}">
    <title>用户管理</title>
</head>
<body>
<div class="row-2">
    <nav class="navbar navbar-expand-lg navbar-light bg-light" style="height: 80px">
        <a class="topA navbar-brand" th:href="@{/admin/user}" href="#">用户管理</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02"
                aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
            <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
                <li class="nav-item active">
                    <a class="topA nav-link" th:href="@{/book/book}" href="#">图书信息管理</a>
                </li>
                <li class="nav-item">
                    <a class="topA nav-link" th:href="@{/order/selectAllOrder}" href="#">订单管理</a>
                </li>
            </ul>
            <span th:text="${message}" style="margin-right: 450px;color: red"></span>
            <span class="form-inline my-2 my-lg-0">
                <button id="add" class="btn btn-primary">增加</button>
                <input id="search" name="username" class="form-control mr-sm-2" type="text" placeholder="Search">
                <button th:onclick="userList(1)" class="btn btn-outline-success my-2 my-sm-0" type="submit">查询</button>
            </span>
        </div>
    </nav>
</div>

<div class="row-10">
    <div id="tab" th:fragment="tab">
        <table class="table">
            <thead>
            <tr>
                <th scope="col">序号</th>
                <th scope="col">用户名</th>
                <th scope="col">用户身份</th>
                <th scope="col">用户金钱</th>
                <th scope="col">创建时间</th>
                <th scope="col">操作</th>
            </tr>
            </thead>
            <tbody>
            <tr th:each="user:${users}">
                <th scope="row" th:text="${userStat.count}">1</th>
                <td th:text="${user.username}">Mark</td>
                <td th:text="${user.identity}">Otto</td>
                <td th:text="${user.money}">@mdo</td>
                <td id="time" th:text="${user.createTime}">2000-10-10</td>
                <td>
                    <button type="button" th:onclick="|del(${user.id})|" class="del btn btn-danger">删除</button>
                    <button type="button" th:onclick="|motai(${user.id})|" class="upd btn btn-dark">修改</button>
                    <!--模态窗口-->
                    <div id="myMo" class="modal" tabindex="-1">
                        <div class="container">
                            <div class="modal-content">
                                <div class="modal-body">
                                    用户名：<input type="text" id="username" name="username"><br>
                                    密码：<input type="password" id="password" name="password"><br>
                                    用户身份：(只能填 普通用户 或者 管理员)<input type="text" id="identity" name="identity"><br>
                                    用户金钱：<input type="text" id="money" name="money"><br>
                                    <button th:onclick="upd()" type="submit">提交</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
            </tbody>
        </table>
    </div>
    <div class="row">
        <div class="offset-5">
            <nav aria-label="Page navigation example">
                <ul class="pagination">
                    <li class="page-item">
                        <a class="page-link" th:href="@{javascript:userList({b})(b=${info.prePage})}"
                           aria-label="Previous">
                            <span aria-hidden="true">&laquo;</span>
                        </a>
                    </li>
                    <li th:each="inf:${#numbers.sequence(1,info.pages,+1)}" class="page-item">
                        <a class="page-link" th:href="@{javascript:userList({b})(b=${inf})}" th:text="${inf}">1</a>
                    </li>
                    <li class="page-item">
                        <a class="page-link" th:href="@{javascript:userList({b})(b=${info.nextPage})}"
                           aria-label="Next">
                            <span aria-hidden="true">&raquo;</span>
                        </a>
                    </li>
                </ul>
            </nav>
        </div>
    </div>
</div>
<!--模态窗口-->
<div id="myModal" class="modal" tabindex="-1">
    <div class="container">
        <div class="modal-content">
            <div class="modal-body">
                <form th:action="@{/admin/insert}">
                    用户名：<input type="text" name="username"><br>
                    密码：<input type="password" name="password"><br>
                    用户身份：(只能填 普通用户 或者 管理员)<input type="text" name="identity"><br>
                    用户金钱：<input type="text" name="money"><br>
                    <button type="submit">提交</button>
                </form>
            </div>
        </div>
    </div>
</div>


<!-- JavaScript 文件是可选的。从以下两种建议中选择一个即可！ -->

<!-- 选项 1：jQuery 和 Bootstrap 集成包（集成了 Popper） -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js"></script>

<script>
    <!-- 用户列表分页-->
    function userList(pageNum) {
        var username = $("#search").val();
        $.ajax({
            url: '/bookCity/admin/userPage',
            data: {
                pageNum: pageNum,
                username: username
            },
            dataType: 'json',
            success: function (data) {
                console.log(data);
                let first = `<table class="table">
        <thead>
        <tr>
            <th scope="col">序号</th>
            <th scope="col">用户名</th>
            <th scope="col">用户身份</th>
            <th scope="col">用户金钱</th>
            <th scope="col">创建时间</th>
            <th scope="col">操作</th>
        </tr>
        </thead>`;
                var str = '';
                let last = '</table>'
                $.each(data.list, function (index, item) {
                    str += `
        <tbody id="tbody">
        <tr>
            <th scope="row" >${item.id}</th>
            <td >${item.username}</td>
            <td >${item.identity}</td>
            <td >${item.money}</td>
            <td >${item.createTime}</td>
            <td>
                <button onclick="del(${item.id})" type="button" class="del btn btn-danger">删除</button>
                <button type="button" onclick="motai(${item.id})" class="upd btn btn-dark">修改</button>
                <div id="myMo" class="modal" tabindex="-1">
                    <div class="container">
                        <div class="modal-content">
                            <div class="modal-body">
                                    用户名：<input type="text" id="username" name="username"><br>
                                    密码：<input type="password" id="password" name="password"><br>
                                    用户身份：<input type="text" id="identity" name="identity"><br>
                                    用户金钱：<input type="text" id="money" name="money"><br>
                                    <button onclick="upd()" type="submit">提交</button>
                            </div>
                        </div>
                    </div>
                </div>
            </td>
        </tr>
        </tbody>`
                })
                $(".table").html(first + str + last);
            }
        })
    }

    function del(id) {
        $.ajax({
            url: '/bookCity/admin/delete',
            data: {id: id},
            success: function (data) {
                alert("删除成功！");
                $("#tab").html(data);
            }
        })
    }

    //模态窗口 因为thymeleaf没刷新这里，所以不需要处理
    $('#add').click(function () {
        $('#myModal').modal('show')
        ;
    });

    var id;

    function motai(i) {
        id = i
        $('#myMo').modal('show');
    }

    function upd() {
        var username = $("#username").val();
        var password = $("#password").val();
        var identity = $("#identity").val();
        var money = $("#money").val();
        $.ajax({
            url: '/bookCity/admin/update',
            data: {
                id: id,
                username: username,
                password: password,
                identity: identity,
                money: money,
            },
            success: function (data) {
                $("#tab").html(data);
            }
        })
        $("#myMo").modal('hide');
    }


</script>
</body>
</html>
